package mblog.core.persist.dao.impl;

import java.util.List;

import mblog.core.persist.dao.ProductRefDao;
import mtons.modules.persist.impl.DaoImpl;

import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.faxsun.core.persist.entity.ProductRefPO;

public class ProductRefDaoImpl extends DaoImpl<ProductRefPO> implements
		ProductRefDao {

	/**
	 * 
	 */
	private static final long serialVersionUID = -922371976716561770L;

	protected ProductRefDaoImpl() {
		super(ProductRefPO.class);
	}

	@Override
	public Long getMaxProductId() {
		Criteria c = this.createCriteria();
		c.setProjection(Projections.max("productId"));
		Long maxId = (Long) c.list().get(0);
		return maxId == null ? 0L : maxId;
	}

    @Override
    public ProductRefPO findById(long refId) {
        TopQuery<ProductRefPO> q = topQuery(1);
        q.add(Restrictions.eq("id", refId));
        List<ProductRefPO> list = q.list();
        return (list != null && list.size() > 0) ? list.get(0) : null;
    }

}
